package com.fastloan.mgt.daily.service.impl;

import com.fastloan.common.functioninterface.SuFunction;
import com.fastloan.mgt.daily.dao.WorkContentMapper;
import com.fastloan.mgt.daily.dao.WorkLocationMapper;
import com.fastloan.mgt.daily.model.vo.WorkContentVo;
import com.fastloan.mgt.daily.model.vo.WorkLocationVo;
import com.fastloan.mgt.daily.service.WorkInfoService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.utils.DozerUtil;
import com.kawins.result.ResultTo;
import com.kawins.shiro.temp.util.ShiroKit;
import com.kawins.supers.tree.Bean2TreeUtil;
import com.kawins.supers.tree.exp.TreeInitializeException;
import com.kawins.util.colle.ListSortUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.toList;

/**
 * @author yanwe
 *         createTime 2017-09-2017/9/12 17:37
 */
@Service
public class WorkInfoServiceImpl implements WorkInfoService {

    @Autowired
    private WorkContentMapper workContentMapper;

    @Autowired
    private WorkLocationMapper workLocationMapper;

    @Override
    public WorkContentMapper getWorkContentMapper() {
        return workContentMapper;
    }

    @Override
    public WorkLocationMapper getWorkLocationMapper() {
        return workLocationMapper;
    }

    @Override
    public ResultTo getWorkInfo(String orgId){
        ResultTo resultTo = new ResultTo();
        CompletableFuture<List<WorkContentVo>> workContentFuture =
                CompletableFuture.supplyAsync(() -> getWorkContent(orgId,0,null));
        String userId = ManagerKit.getUserId();
        CompletableFuture<List<WorkLocationVo>> workLocationFuture =
                CompletableFuture.supplyAsync(() -> getWorkLocation(orgId,0,userId));
        resultTo.setData("workContent",workContentFuture.join())
                .setData("workLocation",workLocationFuture.join());
        return resultTo;
    }

    @Override
    public List<WorkContentVo> getWorkContent(String orgId,Integer id,String userId){
        return convertTree(workContentMapper::selectWorkContent,WorkContentVo.class,orgId,id,userId);
    }

    @Override
    public List<WorkLocationVo> getWorkLocation(String orgId,Integer id,String userId){
        return convertTree(workLocationMapper::selectWorkLocation,WorkLocationVo.class,orgId,id,userId);
    }

    /**
     * 转换数据
     *
     * @param biFunction 数据源
     * @param clazz 转换的树对象
     * @param orgId 机构id
     * @param rootId rootId
     * @param userId 用户id 非必填
     * @param <R> VO类型
     * @param <T> PO类型
     * @return 转换后的列表
     */
    private <R,T> List<R> convertTree(SuFunction<String, Integer,String, List<T>> biFunction
            , Class<R> clazz, String orgId,Integer rootId,String userId){
        List<R> collect = biFunction.apply(orgId,rootId,userId).stream()
                .map(sourceBean -> DozerUtil.map(sourceBean, clazz))
                .collect(toList());
        LinkedHashMap<String,Integer> orderMap = new LinkedHashMap<>();
        orderMap.put("userId", ListSortUtil.DESC);
        orderMap.put("createTime",ListSortUtil.DESC);
        Bean2TreeUtil<R> treeUtil = new Bean2TreeUtil<>(orderMap);
        if (collect.isEmpty()){
            return new ArrayList<>();
        }
        return treeUtil.getChildNodesByParentId(collect, 0);
    }
}
